/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.common.systemannouncement.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.common.systemannouncement.entity.SystemAnnouncement;
import cn.apestar.modules.common.systemannouncement.service.SystemAnnouncementApiService;
import cn.apestar.modules.common.systemannouncement.service.SystemAnnouncementService;

/**
 * 系统公告Controller
 * @author yudw
 * @version 2016-08-20
 */
@Controller
@RequestMapping(value = "${adminPath}/systemannouncement/systemAnnouncement")
public class SystemAnnouncementController extends BaseController {

    @Autowired
    private SystemAnnouncementService systemAnnouncementService;

    @Autowired
    private SystemAnnouncementApiService systemAnnouncementApiService;

    @ModelAttribute
    public SystemAnnouncement get(@RequestParam(required = false) String id) {
        SystemAnnouncement entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = systemAnnouncementService.get(id);
        }
        if (entity == null) {
            entity = new SystemAnnouncement();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(SystemAnnouncement systemAnnouncement, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<SystemAnnouncement> page = systemAnnouncementService.findPage(new Page<SystemAnnouncement>(request, response), systemAnnouncement);
        model.addAttribute("page", page);
        return "modules/systemannouncement/systemAnnouncementList";
    }

    @RequestMapping(value = "form")
    public String form(SystemAnnouncement systemAnnouncement, Model model) {
        model.addAttribute("systemAnnouncement", systemAnnouncement);
        return "modules/systemannouncement/systemAnnouncementForm";
    }

    @RequestMapping(value = "sendPush")
    public String sendPush(SystemAnnouncement systemAnnouncement, Model model) {
        systemAnnouncementApiService.sendPush("系统消息", systemAnnouncement.getContent());
        return "redirect:" + Global.getAdminPath() + "/systemannouncement/systemAnnouncement/list";
    }

    @RequestMapping(value = "save")
    public String save(SystemAnnouncement systemAnnouncement, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, systemAnnouncement)) {
            return form(systemAnnouncement, model);
        }
        systemAnnouncementService.save(systemAnnouncement);
        addMessage(redirectAttributes, "保存系统公告成功");
        return "redirect:" + Global.getAdminPath() + "/systemannouncement/systemAnnouncement/list";
    }

    @RequestMapping(value = "delete")
    public String delete(SystemAnnouncement systemAnnouncement, RedirectAttributes redirectAttributes) {
        systemAnnouncementService.delete(systemAnnouncement);
        addMessage(redirectAttributes, "删除系统公告成功");
        return "redirect:" + Global.getAdminPath() + "/systemannouncement/systemAnnouncement/list";
    }

}